package cn.szzsi._admin;

import cn.szzsi.core.kit.UtilDate;
import cn.szzsi.service.UserBalanceDetailService;
import cn.szzsi.common.bean.dto.Msg;
import cn.szzsi.common.excel.PoiRender;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.UserBalanceDetail;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.render.Render;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Created by zhengy on 2017/2/27.
 */
//@ControllerBind(controllerKey = "/shop/balancedetail")
public class UserBalanceDetailController extends BaseAdminController<UserBalanceDetail> {
    private String shopId;
    //培养津贴
    public void pyjtList(){
        setAttr("yesterday", UtilDate.timestampToDateStr(UtilDate.getYestdayStartTime()));
        render("pyjt.html");
    }

    //培养津贴
    public void ajaxPyjtList(){
        shopId=getAttr("shopId");
        Integer rankLevel=getParaToInt("type",1);//几级分红(默认1级分红)
        String profitDate=getPara("profitDate", UtilDate.timestampToDateStr(UtilDate.getYestdayStartTime())); //默认昨天凌晨时间
        Map<String,Object> result = UserBalanceDetailService.service.getPyjtList(rankLevel,shopId,profitDate);
        renderJson(result);
    }
    @Before(Tx.class)
    public void createPyjt(){
        shopId=getAttr("shopId");
        String profitDate=getPara("createDate");//分红日期
        int dataLevel=getParaToInt("dataLevel");//几级分红
        setAttr("dataLevel",dataLevel);

        Long todayStartTime= UtilDate.getTodayStartTime();//今天凌晨的时间戳
        try {
            if(UtilDate.strToTimestamp(profitDate)>=todayStartTime){
                renderJson(Msg.fail("只能生成昨天或者昨天之前的分红金额!",""));
                return;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //判断所选分红日期是否存在
        if(UserBalanceDetailService.service.isExitPyjtRecord(dataLevel,profitDate,shopId).size()>1){
            renderJson(Msg.fail("所选日期已生成分红金额!",""));
            return;
        }
        UserBalanceDetailService.service.createPyjt(shopId,dataLevel,profitDate);//生成分红金额
        renderJson(Msg.SUCCESS);
    }


    //给会员分红培养津贴金额(一键分配)
    @Before(Tx.class)
    public void distributionPyjt(){
        shopId=getAttr("shopId");
        String profitDate=getPara("profitDate");
        Integer rankLevel=getParaToInt("dataLevel");
        if(UserBalanceDetailService.service.isDistributionPyjt(rankLevel,profitDate,shopId).size()<1){
            renderJson(Msg.fail("所选日期已分配!",""));
            return;
        }
        UserBalanceDetailService.service.distributionPyjt(rankLevel,profitDate,shopId);
        renderJson(Msg.SUCCESS);
    }

    //导出培养津贴
    public void exportPyjt(){
        shopId=getAttr("shopId");
        String profitDate=getPara("profitDate");
        Integer rankLevel=getParaToInt("dataLevel");
        List<Record> list = Db.find("select m.code,m.username,m.phone,b.rewardnum,b.addMoney,(case when b.status=1 then '已分配' else '未分配' end)status,b.createDate  from mall_member m right join mall_user_balance_detail b on m.id=b.mid where b.type='1' and b.rankLevel=? and b.profitDate=? and b.shopId=?",rankLevel,profitDate,shopId);
        String[] header={"会员ID","会员昵称","联系方式","直荐会员数","分红金额","分红状态","日期"};
        String[] columns={"code","username","phone","rewardnum","addMoney","status","createDate"};
        Render poirender = PoiRender.me(list).fileName("profit("+profitDate+").xls").headers(header).sheetName("培养津贴").columns(columns);
        render(poirender);
    }

    //辅导津贴
    public void fdjtList(){
        render("fdjt.html");
    }
    public void ajaxFdjtList(){
        shopId=getAttr("shopId");
        Integer rankLevel=getParaToInt("type",1);//几级分红(默认1级分红)
        Map<String,Object> result = UserBalanceDetailService.service.getFdjtList(rankLevel,shopId);
        renderJson(result);
    }


}
